﻿using System;
using System.Windows.Forms;
using xClient.Framework;
using xClient.Framework.BaseView;
using xClient.Framework.PlugCore;

namespace cMES1006
{
    [Plug(ID ="cMES1006",
        Name ="线体基础信息",
        Description ="线体基础资料维护",
        Parent ="0,基础资料")]
    public partial class cMES1006 : StdView
    {
        public cMES1006()
        {
            InitializeComponent();
          
        }


        private void cMES1006_Load(object sender, EventArgs e)
        {
            //initial line base info
            this.LoadLineBaseInfo();
        }
        private void LoadLineBaseInfo()
        {
            ExecutionResult<dynamic> _res;
            _res = Services.ServiceMethod.GetLineBaseInfoList();
            if (!_res.Status)
            {
                ShowMessage(_res.Message, MsgType.NG);
                return;
            }
            datalist.DataSource = _res.Data;
            datalist.ClearSelection();
            
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            var _line = this.txtSearch.Text.Trim().ToUpper();
            var _shift = this.combShift.Text.Trim().ToUpper();

            var _res = Services.ServiceMethod.GetLineBaseInfoList(_line, _shift);
            if (!_res.Status)
            {
                ShowMessage(_res.Message, MsgType.NG);
                return;
            }
            if (_res.Data.Count > 0)
            {
                datalist.DataSource = _res.Data;
            }
            else
            {
                datalist.Rows.Clear();
            }
            datalist.ClearSelection();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            this.LineBaseCfgChange(sender);
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            this.LineBaseCfgChange(sender);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (datalist.SelectedRows.Count < 1)
            {
                this.ShowMessage("请选择一笔需要删除的线体资料..", MsgType.NG);
                return;
            }
            var line_id = datalist.SelectedRows[0].Cells[0].Value;
            var line_name = datalist.SelectedRows[0].Cells[1].Value;

            DialogResult dialog = MessageBox.Show(
                "确定要删除 " + line_name + "吗?", "系统提示",
                MessageBoxButtons.YesNo,
                 MessageBoxIcon.Warning,
                  MessageBoxDefaultButton.Button2);
            if(dialog == DialogResult.Yes)
            {
                var result = Services.ServiceMethod.DeleteLineBase(line_id.ToString());
                if(result.Status)
                {
                    this.ShowMessage("删除数据成功..", MsgType.OK);
                    //刷新数据
                    this.LoadLineBaseInfo();
                }
            }
        }

        private void datalist_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if(e.RowIndex > -1)
            {
                //修改数据
                frmLineBaseEdit lineEdit = new frmLineBaseEdit("EDIT", datalist.Rows[e.RowIndex]);

                //lineEdit.ShowDialog();
                DialogResult dialog = lineEdit.ShowDialog();
                if (dialog == DialogResult.OK)
                {
                    this.ShowMessage("修改成功.", MsgType.OK);
                }
                else  if (dialog == DialogResult.No)
                {
                    this.ShowMessage("修改成功.", MsgType.OK);
                }
                //刷新数据
                this.LoadLineBaseInfo();
            }
        }

        private void LineBaseCfgChange(object sender)
        {
            frmLineBaseEdit _lineEdit = default(frmLineBaseEdit);
            var clickBtnTag = ((ToolStripButton)sender).Tag;
            if (clickBtnTag.Equals("ADD"))
            {
                _lineEdit = new frmLineBaseEdit(clickBtnTag.ToString());
                DialogResult result = _lineEdit.ShowDialog();
                if(result == DialogResult.OK)
                {
                    this.ShowMessage("数据新增成功..", MsgType.OK);
                    //refresh data
                    this.LoadLineBaseInfo();
                }
                if (result == DialogResult.No)
                {
                    this.ShowMessage("数据新增失败..", MsgType.NG);
                    //refresh data
                    this.LoadLineBaseInfo();
                }
            }
            if (clickBtnTag.Equals("EDIT"))
            {
                if (datalist.SelectedRows.Count < 1)
                {
                    this.ShowMessage("请选择一笔需要修改的线体资料..", MsgType.NG);
                    return;
                }
                _lineEdit = new frmLineBaseEdit(clickBtnTag.ToString(),datalist.SelectedRows[0]);
                DialogResult result = _lineEdit.ShowDialog();
                if (result == DialogResult.OK)
                {
                    this.ShowMessage("数据修改成功..", MsgType.OK);
                    //refresh data
                    this.LoadLineBaseInfo();
                }
                if (result == DialogResult.No)
                {
                    this.ShowMessage("数据修改失败..", MsgType.NG);
                    //refresh data
                    this.LoadLineBaseInfo();
                }
            }
        }

    }
}
